package cn.com.egova.skinapplication.skin;

import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

/**
 * Created by yuanchao on 2017/3/12.
 */

public class ResourceManager {
    private Resources mResources;
    private String mPkgName;
    private String mSuffix;

    public ResourceManager(Resources mResources, String mPkgName, String suffix) {
        this.mResources = mResources;
        this.mPkgName = mPkgName;
        if (suffix == null) {
            suffix = "";
        }
        this.mSuffix = suffix;
    }


    /**
     * 根据资源名字获取Drawable资源.
     *
     * @param resName
     * @return
     */
    public Drawable getDrawableByResName(String resName) {
        try {
            resName = appendSuffix(resName);
            Drawable drawable = mResources.getDrawable(mResources.getIdentifier(resName, "drawable", mPkgName));
            return drawable;
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    private String appendSuffix(String name) {
        if (!TextUtils.isEmpty(mSuffix)) {
            name += mSuffix;
        }
        return name;
    }

    /**
     * 根据资源名字获取Color资源.
     *
     * @param resName
     * @return
     */
    public ColorStateList getColorByResName(String resName) {
        try {
            resName = appendSuffix(resName);
            ColorStateList colorStateList = mResources.getColorStateList(mResources.getIdentifier(resName, "color", mPkgName));
            return colorStateList;
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}
